﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Module.Category.BL;
using QLBH.DTO;
using QLBH.Model.Controller;
using QLBH.DataModel;
using Module.Category.Properties;
using QLBH.Base;

namespace Module.Category
{
    public enum eDialogType
    {
        DL_ADD,
        DL_EDIT
    };
    public partial class AddProductDlg : Form
    {
        private eDialogType dialogType;

        public eDialogType DialogType
        {
            get { return dialogType; }
            set 
            { 
                dialogType = value;
                if (dialogType == eDialogType.DL_ADD)
                {
                    this.Text = "Thêm hàng";
                    this.Icon = Icon.FromHandle(Resources.add_24.GetHicon());
                }
                else
                {
                    this.Text = "Sửa thông tin hàng hóa";
                    this.Icon = Icon.FromHandle(Resources.write_24.GetHicon());
                }
            }
        }
        private HangHoaDTO newProduct;

        public HangHoaDTO NewProduct
        {
            get { return newProduct; }
            set 
            { 
                newProduct = value;

            }
        }
        public AddProductDlg()
        {
            InitializeComponent();
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            if (IsValidValue())
            {
                try
                {
                    if (dialogType == eDialogType.DL_ADD)
                    {
                        AddProduct();
                    }
                    else
                    {
                        UpdateProduct();
                    }


                    DialogResult = DialogResult.OK;
                    this.Close();
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show(ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private bool IsValidValue()
        {
            // Product Name
            try
            {
                DataInputHelper.CheckTextData(txtProductName.Text, "Tên MH");
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtProductName.SelectAll();
                txtProductName.Focus();
                return false;
            }
            // Ngay SX
            try
            {
                DataInputHelper.CheckTextData(dtpStartDate.Text, "Ngày SX");
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                dtpStartDate.Select();
                dtpStartDate.Focus();
                return false;
            }
            // Han SD
            try
            {
                DataInputHelper.CheckTextData(dtpEnddate.Text, "Hạn SD");
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                dtpEnddate.Select();
                dtpEnddate.Focus();
                return false;
            }
            // Gia Nhap
            try
            {
                DataInputHelper.CheckIntValue(txtGiaMua.Text, "Giá mua");
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtGiaMua.SelectAll();
                txtGiaMua.Focus();
                return false;
            }
            // Gia Ban
            try
            {
                DataInputHelper.CheckIntValue(txtGiaBan.Text, "Giá bán");
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtGiaBan.SelectAll();
                txtGiaBan.Focus();
                return false;
            }
           
            //SLuong
            try
            {
                DataInputHelper.CheckIntValue(txtNumberProduct.Text, "Số lượng");
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtNumberProduct.SelectAll();
                txtNumberProduct.Focus();
                return false;
            }
            if (cbCalUnit.SelectedItem==null)
            {
                MessageBox.Show("Bạn phải chọn đơn vị tính", "Lỗi",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                cbCalUnit.SelectAll();
                cbCalUnit.Focus();
                return false;
            }
            if (cbProductCategory.SelectedItem == null)
            {
                MessageBox.Show("Bạn phải chọn nhóm hàng", "Lỗi",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                cbProductCategory.SelectAll();
                cbProductCategory.Focus();
                return false;
            }
            if (cbProvider.SelectedItem == null)
            {
                MessageBox.Show("Bạn phải chọn nhà cung cấp", "Lỗi",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                cbProvider.SelectAll();
                cbProvider.Focus();
                return false;
            }
            if (dtpEnddate.Value<dtpStartDate.Value)
            {
                MessageBox.Show("Hạn sử dụng phải lớn hơn ngày sx", "Lỗi",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
            return true;
        }

        private void UpdateProduct()
        {
            newProduct.GiaBan = long.Parse(txtGiaBan.Text);
            newProduct.GiaNhap = long.Parse(txtGiaMua.Text);
            newProduct.HanSD = dtpEnddate.Value;
            newProduct.NgaySX = dtpStartDate.Value;
            newProduct.DVTId = (int)cbCalUnit.SelectedValue;

            newProduct.NhaCCId = (int)cbProvider.SelectedValue;
            newProduct.NhomHangId = (int)cbProductCategory.SelectedValue;
            newProduct.TenMH = txtProductName.Text;
            newProduct.MaVach = txtProductCode.Text;

            HangHoa updatedproduct = newProduct.ToEntity();
            ProductController.Instance.Edit(updatedproduct);
        }

        private void AddProduct()
        {
            newProduct = new HangHoaDTO();
            newProduct.GiaBan = long.Parse(txtGiaBan.Text);
            newProduct.GiaNhap = long.Parse(txtGiaMua.Text);
            newProduct.HanSD = dtpEnddate.Value;
            newProduct.NgaySX = dtpStartDate.Value;
            newProduct.DVTId = (int)cbCalUnit.SelectedValue;

            newProduct.NhaCCId = (int)cbProvider.SelectedValue;
            newProduct.NhomHangId = (int)cbProductCategory.SelectedValue;
            newProduct.TenMH = txtProductName.Text;
            newProduct.MaVach = txtProductCode.Text;

            AddNewProduct();
        }
        private void AddNewProduct()
        {
            HangHoa newProductEntities=NewProduct.ToEntity();
            newProduct = ProductController.Instance.Add(newProductEntities).ToDTO();
        }


        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void AddProductDlg_Load(object sender, EventArgs e)
        {
            GetListProductCategory();
            GetListProvider();
            GetListCalcUnits();
            dtpStartDate.Value = DateTime.Now;
            dtpEnddate.Value = DateTime.Now;
            if (dialogType==eDialogType.DL_EDIT && newProduct!=null)
            {
                txtGiaBan.Text = newProduct.GiaBan.ToString();
                txtGiaMua.Text = newProduct.GiaNhap.ToString();
                txtProductCode.Text = newProduct.MaVach;
                txtProductName.Text = newProduct.TenMH;
                txtNumberProduct.Text = newProduct.SLTon.ToString();

                dtpEnddate.Value = newProduct.HanSD;
                dtpStartDate.Value = newProduct.NgaySX;

                cbCalUnit.SelectedValue = NewProduct.DVTId;
                cbProductCategory.SelectedValue = NewProduct.NhomHangId;
                cbProvider.SelectedValue = newProduct.NhaCCId;
            }
        }

        private void labelX12_Click(object sender, EventArgs e)
        {
            AddProductCategoryDlg addProductCategoryDlg = new AddProductCategoryDlg();
            if (addProductCategoryDlg.ShowDialog()==DialogResult.OK)
            {
                GetListProductCategory();
                cbProductCategory.SelectedValue = addProductCategoryDlg.ProductCategory.Id;
            }
        }
        private void GetListProductCategory()
        {
            //Get List Product Category
            List<NhomHangDTO> listProductCategories = ProductCategoryManagementBL.Instance.GetAllProductCategories().ToList();
            cbProductCategory.DataSource = listProductCategories;
            cbProductCategory.DisplayMember = "TenNH";
            cbProductCategory.ValueMember = "Id";
        }
        private void GetListProvider()
        {
            //Get List Provider
            List<NhaCCDTO> listProviders = ProviderManagementBL.Instance.GetAllProviders().ToList();
            cbProvider.DataSource = listProviders;
            cbProvider.DisplayMember = "TenNCC";
            cbProvider.ValueMember = "Id";
        }
        private void GetListCalcUnits()
        {
            //Get List Calculate Unit
            List<DVTDTO> listCalUnits = CalculateUnitManagementBL.Instance.GetAllCalUnits().ToList();
            cbCalUnit.DataSource = listCalUnits;
            cbCalUnit.DisplayMember = "TenDV";
            cbCalUnit.ValueMember = "Id";
        }

        private void labelX11_Click(object sender, EventArgs e)
        {
            AddProvider addproviderDlg = new AddProvider();
            if (addproviderDlg.ShowDialog() == DialogResult.OK)
            {
                GetListProvider();
                cbProvider.SelectedValue = addproviderDlg.Provider.Id;
            }
        }

        private void labelX13_Click(object sender, EventArgs e)
        {
            AddCalUnitDlg addCalUnitDlg = new AddCalUnitDlg();
            if (addCalUnitDlg.ShowDialog() == DialogResult.OK)
            {
                GetListCalcUnits();
                cbCalUnit.SelectedValue = addCalUnitDlg.CalUnit.Id;
            }
        }
    }
}
